package com.woniuxy.driverschool.controller;

import com.woniuxy.driverschool.entity.CredentialManagement;
import com.woniuxy.driverschool.entity.PageBean;
import com.woniuxy.driverschool.entity.ResponseResult;
import com.woniuxy.driverschool.entity.Student;
import com.woniuxy.driverschool.service.CredentialManagementService;
import com.woniuxy.driverschool.service.impl.CredentialManagementServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 证件管理
 * @author shkstart
 * @create 2021-10-12 16:45
 */
@RestController
public class CredentialManagementController {
    @Autowired
    CredentialManagementService credentialManagementService;

    /**
     * 分页查询驾证管理学员领证状态
     * @param credentialManagement
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @PostMapping("/getCard/{pageIndex}/{pageSize}")
    public ResponseResult<PageBean<CredentialManagement>> getCardStudent(@RequestBody CredentialManagement credentialManagement,
                                                                         @PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize")int pageSize){
        System.out.println("查询条件："+credentialManagement);
        System.out.println("pageSize："+pageSize);
        System.out.println("pageIndex："+pageIndex);
        int selectCountCredentialManagementByPage = credentialManagementService.selectCountCredentialManagementByPage(credentialManagement,null, null);
        List<CredentialManagement> students = credentialManagementService.selectCredentialManagementByPage(credentialManagement, pageIndex, pageSize, null, null);
        PageBean<CredentialManagement> pageBean = new PageBean<CredentialManagement>();
        pageBean.setBeanList(students);
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalRecord(selectCountCredentialManagementByPage);
        return new ResponseResult<PageBean<CredentialManagement>>(pageBean,200,"ok");
    }

    /**
     *修改领证状态
     * @param credentialManagement
     * @return
     */
    @PutMapping("/getStatus")
    public ResponseResult updategetStatus(@RequestBody CredentialManagement credentialManagement){
        credentialManagement.setGetStatus("已领取");
        credentialManagement.setGetTime(new Date());
        credentialManagementService.updateCredentialManagement(credentialManagement);
        System.out.println("修改成功："+credentialManagement);
        return new ResponseResult(null,200,"ok");
    }
    /**
     * 前端日期串转换成日期类
     * @param request
     * @param binder
     */
    @InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder ) {
        SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
        dateFormat.setLenient( false );
        binder.registerCustomEditor( Date.class, new CustomDateEditor( dateFormat, false ) );
    }
}

